package com.situ.mymall.service.impl;

import com.situ.mymall.mapper.CartMapper;
import com.situ.mymall.pojo.entity.Cart;
import com.situ.mymall.pojo.query.CartQuery;
import com.situ.mymall.pojo.vo.CartVO;
import com.situ.mymall.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceImpl implements ICartService {

    @Autowired
    private CartMapper cartMapper;

    //前端添加
    @Override
    public void add(Cart cart) {

        int cartCount = cartMapper.selectCountByUserIdAndProductId(cart);
        //这个商品已经在这个用户的购物车里面，只要更新数量就可以
        if (cartCount == 1){
            cartMapper.updateQuantity(cart);
        }else{
            //这个商品在这个用户的购物车里面没有，就直接插入
            cartMapper.insertSelective(cart);
        }

    }

    //前端展示
    @Override
    public List<CartVO> list(CartQuery cartQuery) {
        return cartMapper.list(cartQuery);
    }

    @Override
    public void update(Cart cart) {
        cartMapper.updateByPrimaryKeySelective(cart);
    }

    @Override
    public void deleteById(Integer id) {
        cartMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateByUserId(Cart cart) {
        cartMapper.updateByUserId(cart);
    }
}
